import React, { Component } from 'react';
import {
	observer,
} from 'mobx-react';
import { List } from 'weaver-mobile';
import { Form } from 'weaver-mobile-page';
import {
  withRouter,
} from 'react-router-dom';

@withRouter
@observer
export default class HrmForm extends Component {
	getFields = () => {
    const {form, linkages} = this.props;
    const { conditioninfo, isInit } = form;
    const fields = [];
    if (isInit) {
      conditioninfo.forEach((group) => {
        group.items.forEach((field) => {
          const key = field.domkey.join('__');

          //表单需要联动
          if (linkages && linkages[key]) {
            const {activeKey,activeValue} = linkages[key];
            const formParams = form.formParams;
            const curValue = formParams[activeKey];
            if (!activeValue.includes(curValue)) {
              return null;
            }
          }

          fields.push(<Form.Item
            key={key}
            form={form}
            fieldConfig={field}
            onChange={(condition, value) => {
             this.handleFormItemChange(value,condition.domkey[0]);
              form.onChange(condition, value);
            }}
          />);
        });
      });
    }
    return fields;
  }

  handleFormItemChange = (value, key) => {
    //将岗位级别切换至总部
    if ( key == "sharetype"  && value !== "7") {
      const {
        form
      } = this.props;
      form.updateField("jobtitlelevel", "0");
    }
  }

	render() {
		return (
			 <Form style={{ height: '100%' }} >
		        <List>
		          {this.getFields()}
		        </List>
		      </Form>
		);
	}
}